<?php

class EventsController extends AppController
{
	var $name = 'Events';
	var $uses = array('Event', 'EventType','Country');
	var $paginate = array('conditions'=>array('Event.published'=>1, 'Event.event_date >= NOW()'),'fields'=>array('Event.id', 'Event.venue','Event.address','Event.city','Event.country_id','Event.event_date','Event.event_time','EventType.name','Country.id','Country.name'),'limit'=>10, 'order'=>array('Event.event_date'=>'ASC'));
	
	//var $cacheAction = '1 hour';	
	
	function index()
	{
		$events = $this->paginate();
		$this->set(compact('events'));
	}
	
	function view($id)
	{
		$event = $this->Event->find( array('Event.id'=>$id), array('Event.id','Event.event_url','Event.venue','Event.address','Event.city','Event.country_id','Event.postcode','Event.event_date','Event.event_time','Event.notes','EventType.id','EventType.name','Country.id','Country.name'));
		if ($event['Event']['event_url'] == null)
		{
			$event['Event']['event_url'] = 'http://webdev/events/view/' . $event['Event']['id'];
		}
		
		$this->set('event', $event);
		
		$geocode = $this->YahooGeo->generate(null, $event['Event']['address'], null, null, null,  $event['Event']['city'] . ',' . $event['Country']['name']);
		$geo = array();
		if (isset($geocode['Latitude']) &&  isset($geocode['Longitude']))
		{
			$geo['lat'] = $geocode['Latitude'];
			$geo['long'] = $geocode['Longitude'];
		} else {
			$geo['lat'] = 0;
			$geo['long'] = 0;
		}
		$this->set(compact('geo'));
	}
	
	function admin_index()
	{
		$events = $this->Event->findAll();
		$this->set('events', $events);
	}
	
	/**
	 * Function getposts grabs x posts from database
	 * @int num
	 */
	function getevents($num = 5)
	{
		$getevents = $this->Event->findAll('Event.published = 1 AND Event.event_date >= NOW()', array('Event.id','Event.event_url','Event.venue','Event.address','Event.city','Event.country_id','Event.event_date','Event.event_time','Event.notes','EventType.id','EventType.name', 'Country.id', 'Country.name'),'Event.event_date ASC', $num);
		
		if(isset($this->params['requested'])) {
			return $getevents;
		} else {
			$this->Session->setFlash('This page cannot be accessed');	
		}
	}
	
	function admin_add()
	{
		$event_type = $this->Event->EventType->generateList();
		$this->set(compact('event_type'));
		
		$country = $this->Country->generateList(null, 'Country.id ASC', null, '{n}.Country.id', '{n}.Country.name');
		$this->set(compact('country'));
			
		if (isset($this->data))
		{
			$this->cleanUpFields('Event');	
			$this->Event->create();
			if($this->Event->save($this->data))
			{
				$this->Session->setFlash('Event saved');
				$this->redirect(array('action'=>'index'), null, true);
			}
		}
	}
	
	function admin_edit($id)
	{
		if (isset($this->data))
		{
			$this->cleanUpFields('Event');
			if ($this->Event->save($this->data['Event']))
        	{
            	$this->Session->setFlash('Your event has been updated.');
				$this->redirect(array('action'=>'index'), null, true);
        	}
		}
		
		$event_type = $this->Event->EventType->generateList();
		$this->set(compact('event_type'));
			
		$country = $this->Country->generateList(null, 'Country.id ASC', null, '{n}.Country.id', '{n}.Country.name');
		$this->set(compact('country'));
			
		$this->Event->id = $id;
        $this->data = $this->Event->read();
	}
	
	function admin_delete($id)
	{
		$this->Event->delete(array('Event.id'=>$id));
		$this->Session->setFlash('Event ' . $id . ' Deleted.');
		$this->redirect('/admin/events/index');
	}
	
	function feed()
    {
        $this->layout = 'xml';
        $events = $this->Event->findAll('Event.published = 1', null, 'Event.created DESC', 15);
		$this->set('events', $events);
    }
	
	function admin_addtype()
	{
		$this->set('exiting_types', $this->EventType->findAll());
		if(isset($this->data))
		{
			$this->EventType->create();
			if($this->EventType->save($this->data))
			{
				$this->Session->setFlash('Event Type Added');
			}
		}
	}
	
}


?>